/*****************************************************************************
 * Converters.c
 *
 * To flash the bootloader:
 * 1) Project > Project Options > Type: Loader File
 * 2) Rebuild Project
 * 3) Tools > Flash Programmer.. > Browse > 
 *        ..\ADSP-21369 EZ-KIT Lite\Flash Programmer\Parallel\21369EzFlashDriver_Parallel.dxe
 * 4) Hit Load
 * 5) Click programming tab and find the linker file. Click 'Program'
 * 6) Program will now boot on power-up
 *
 *****************************************************************************/
 
#include <cdef21369.h>
#include <def21369.h>

//#define SRUDEBUG  // Check SRU Routings for errors.
#include <SRU.h>

void SetupADC(void);
void SetupDAC(void);
void SetCS(int);
void SetupGPIO(void);
void InitConversion(void);
long ReadSPI(void);
void WriteSPI(unsigned long);
void delay(int);


/*	Servo Pin Arrangements

  DPI Header
  
    --------
    --1--2--
    --3--4--
    --5--6--
    --7--8--
    --9--10-
    -11--12-
    -13--14-
    -15--16-
    -17--18-
    -19--20-
    --------
    
Servo Function / DPI Location
!!! Be Careful to assign correct functions to the correct
  locations otherwise you could damage the DACs/ADCs. Always
  remove the header and check with a oscilloscope first. 
  And Disable the pins you do not need !!!

1 GND				      	2 GND
3 DAC SDI 	    (DPI_01) 	4 ADC CLKR		(DPI_02)
5 DAC SCLK 	    (DPI_03)   	6 NC			(DPI_04)
7 NC	 	    (DPI_05)   	8 DAC 01 CS		(DPI_06)
9 3.3V				      	10 3.3V
11 DAC 02 CS    (DPI_07) 	12 DAC 03 CS	(DPI_08)
13 USBTX/UARTRX (DPI_09)   	14 USBRX/UARTTX	(DPI_10)
15 ADC DRR	    (DPI_11)   	16 ADC FSX		(DPI_12)
17 ADC CS	    (DPI_13) 	18 NC			(DPI_14)
19 3.3V				   		20 3.3V
	

*/

void SetupADC()
{
	// Assign Functions to Pins
	
    SRU2(SPI_CLK_O,DPI_PB02_I);
    SRU2(SPI_FLG0_O,DPI_PB13_I);
    //SRU2(SPI_MOSI_O,DPI_PB04_I);

    //SRU2(SPI_MISO_O,DPI_PB11_I);
    SRU2(DPI_PB11_O,SPI_MISO_I);
    
	// Enable the Pin Buffers
	//SRU2(SPI_MISO_PBEN_O,DPI_PBEN11_I);
	SRU2(LOW,DPI_PBEN11_I);
    SRU2(SPI_CLK_PBEN_O,DPI_PBEN02_I);
    SRU2(SPI_FLG0_PBEN_O,DPI_PBEN13_I);
    //SRU2(SPI_MOSI_PBEN_O,DPI_PBEN04_I);
    
    // Disable pins that are not connected (NC)
    //SRU2(LOW, DPI_PBEN04_I);
    //SRU2(LOW, DPI_PBEN05_I);
    //SRU2(LOW, DPI_PBEN14_I);
    
    // Disable all remaining unused pins on the DPI port
    // to ensure nothing is damaged. These get used elsewhere..
    //SRU2(LOW, DPI_PBEN01_I);
    //SRU2(LOW, DPI_PBEN03_I);
    //SRU2(LOW, DPI_PBEN06_I);
    //SRU2(LOW, DPI_PBEN07_I);
    //SRU2(LOW, DPI_PBEN08_I);
    //SRU2(LOW, DPI_PBEN09_I);
    //SRU2(LOW, DPI_PBEN10_I);
    //SRU2(LOW, DPI_PBEN12_I);
    

    /* First configure the SPI Control registers */
    /* First clear a few registers     */
    *pSPICTL = (TXFLSH | RXFLSH) ;
    
    /* Clear the SPI flag register and disable all flags */
    /* See page 706 in the hardware reference for more details */
    *pSPIFLG = 0x0F00;
    /* Clear the Control register */
    *pSPICTL = 0;
    
    /* Setup the baud rate for transmission */
    *pSPIBAUD = 0x0004;
    
    /* Setup the SPI Flag (Chip/Slave Select) register*/
    *pSPIFLG = ( SPIFLG0 | DS0EN );
    
    /* Now setup the SPI Control register */
    /* See page 701 in the hardware reference for more details */
    *pSPICTL = (SPIEN | SPIMS | MSBF | WL32 | TIMOD1) ;

}

void SetupDAC()
{
	// Assign Functions to Pins
	SRU2(SPIB_MOSI_O,DPI_PB01_I);
    SRU2(SPIB_CLK_O,DPI_PB03_I);
    SRU2(SPIB_FLG0_O,DPI_PB06_I);
    SRU2(SPIB_FLG1_O,DPI_PB07_I);
    SRU2(SPIB_FLG2_O,DPI_PB08_I);

	// Enable the Pin Buffers
	SRU2(SPIB_MOSI_PBEN_O,DPI_PBEN01_I);
    SRU2(SPIB_CLK_PBEN_O,DPI_PBEN03_I);
    SRU2(SPIB_FLG0_PBEN_O,DPI_PBEN06_I);
    SRU2(SPIB_FLG1_PBEN_O,DPI_PBEN07_I);
    SRU2(SPIB_FLG2_PBEN_O,DPI_PBEN08_I);
    
    // Disable pins that are not connected (NC)
    SRU2(LOW, DPI_PBEN04_I);
    SRU2(LOW, DPI_PBEN05_I);
    SRU2(LOW, DPI_PBEN14_I);
    
    // Disable all remaining unused pins on the DPI port
    // to ensure nothing is damaged. These get used elsewhere..
    //SRU2(LOW, DPI_PBEN02_I); // ADC
    //SRU2(LOW, DPI_PBEN09_I); // Serial
    //SRU2(LOW, DPI_PBEN10_I); // Serial
    //SRU2(LOW, DPI_PBEN11_I); // ADC
    SRU2(LOW, DPI_PBEN12_I);
    //SRU2(LOW, DPI_PBEN13_I); // ADC
    

    /* First configure the SPI Control registers */
    /* First clear a few registers     */
    *pSPICTLB = (TXFLSH | RXFLSH) ;
    
    /* Clear the SPI flag register and disable all flags */
    /* See page 706 in the hardware reference for more details */
    *pSPIFLGB = 0x0F80;
    /* Clear the Control register */
    *pSPICTLB = 0x00;
    
    /* Setup the baud rate for transmission */
    *pSPIBAUDB = 0x0002;
    
    /* Setup the SPI Flag (Chip/Slave Select) register*/
    *pSPIFLGB = ( SPIFLG0 | DS0EN );
    
    /* Now setup the SPI Control register */
    /* See page 701 in the hardware reference for more details */
    *pSPICTLB = (SPIEN | DMISO | SPIMS | MSBF | WL32 | TIMOD1) ;
    
    /* A 32-bit word length is used. The shift register for the DAC
    is 24-bit. Only the last 18 bits transferred are of importance.
    Because we have selected MSBF (most significant bit first), we
    will only need to use the last 18 of the full transfer. See the
    manual for the DAC8991 chip, page 29. */

}

void SetCS(int flg)
{
	*pSPICTLB = 0x00; //Disable SPI port
	
    *pSPIFLGB = 0x0F80;
    if (flg == 0) *pSPIFLGB = ( 0x0F80 | DS0EN );
    else if (flg == 1) *pSPIFLGB = ( 0x0F80 | DS1EN );
    
    *pSPICTLB = (SPIEN | DMISO | SPIMS | MSBF | WL32 | TIMOD1) ; //Enable SPI port
}

void SetupGPIO()
{
	//assign pin buffer 19&20 low so it is an input
    SRU(LOW,DAI_PB05_I);

	//Route MISCB singnals in SRU_EXT_MISCB (Group E)
    //route so that DAI pin buffer 19&20 connects to MISCB1&MISCB2
    SRU(DAI_PB05_O,MISCB1_I);
    //SRU(DAI_PB20_O,MISCA5_I);

	//Pin Buffer Disable in SRU_PINEN0 (Group F)
    SRU(LOW,PBEN05_I); //assign pin 19 low so it is an input
    SRU(HIGH,PBEN01_I); //assign pin 03 high so it is an output
    
    SRU(LOW,DAI_PB01_I); // set the logic level low
    //*pDAI_PIN_PULLUP ^= DAI_PB03; // turn off the pullup resistor
}

void InitConversion()
{
	SRU(HIGH,DAI_PB01_I); // set the START bit high on the ADC
	// wait for the DRDY bit to indicate that the
	// conversion has completed
	while(!(*pDAI_PIN_STAT & DAI_PB05));
	SRU(LOW,DAI_PB01_I); // pull the START bit to logic low
	/* initiate settle time.. different for each filter setting */
	delay(7);
}


long ReadSPI()
{
	volatile long tmp;
	
	InitConversion(); // start the ADC
	
	//initiate the SPI clock under TIMOD1 by
	//putting data into the transfer buffer
	*pTXSPI = 0x00000000;
	//delay(25);

    //Wait for the SPI to indicate that it has finished.
    while (!(*pSPISTAT & SPIF));
    //delay(50);
    //retrieve the contents of the receive buffer
    while (!(*pSPISTAT & RXS)); //check if the read buffer is full
    tmp = *pRXSPI;

	tmp = tmp << 1; // drop the first bit (junk data)
	/* Make into a legit 32-bit word */
	if ((tmp >> 31) == 1) tmp = 0xFF0000 | (tmp >> 8);
	else tmp = (tmp >> 8);
	return tmp;
}

void WriteSPI(unsigned long value)
{
    *pTXSPIB = value;
    //delay(10);

    //Wait for the SPI to indicate that it has finished.
    while (!(*pSPISTATB & SPIF));
    //delay(10);
}

/////////// MISC FUNCTIONS ///////////
void delay (int i)
{
    for (;i>0;--i)
        asm ("nop;") ;
}
